package com.bana.fvs.app.module.detaile;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.bana.fvs.R;
import com.bana.fvs.app.base.BaseActivity;
import com.bana.fvs.app.common.adapter.CommonAdapter;
import com.bana.fvs.app.common.adapter.ViewHolder;
import com.bana.fvs.app.module.detaile.entity.UpdateWorkModeResultBean;
import com.bana.fvs.app.module.detaile.entity.WorkModeItemBean;
import com.bana.fvs.app.mvp.contract.WorkModeContract;
import com.bana.fvs.app.mvp.presenter.WorkModePresenter;
import com.bana.fvs.app.util.UserInfoUtils;
import com.wei.commonlibrary.net.CommException;
import com.wei.commonlibrary.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by wei on 2018/2/19.
 * <p>
 * 工作模式控制
 */

public class WorkModeControllerActivity extends BaseActivity<WorkModePresenter> implements WorkModeContract.IWrokModeView {
    private static final String TAG = "dah_WorkModeControlle";

    @BindView(R.id.listView)
    ListView mListView;
    @BindView(R.id.btnCommit)
    TextView mBtnCommit;
    private String remoteWorkModel = "0";

    private List<WorkModeItemBean> datas;
    private int switchModel = 0;
    private CommonAdapter<WorkModeItemBean> adapter;
    private int choosedPosition = 0, pageType = 1;
    public static final String param_model = "_param_model_";
    public static final String param_type = "_param_type_";


    /**
     * 启动！
     *
     * @param workModel 工作模式对应的字典值
     * @param pageType  1.雾区 2.能见度检测仪
     */
    public static void startAction(Context context, String workModel, int pageType) {
        Intent intent = new Intent(context, WorkModeControllerActivity.class);
        intent.putExtra(param_model, workModel);
        intent.putExtra(param_type, pageType);
        context.startActivity(intent);
    }

    @Override
    public int setLayout() {
        return R.layout.activity_workmode_controller;
    }

    @Override
    public void setTitle() {
        String title = getString(R.string.title_work_model);
        mBaseTitleBarView.setTitleText(title);
        mBaseTitleBarView.setLeftTitleGone();
        mBaseTitleBarView.setLeftDrawable(-1);
        remoteWorkModel = getIntent().getStringExtra(param_model);//上一页选中的，带过来的
        pageType = getIntent().getIntExtra(param_type, 1);
        Log.e(TAG, "remoteWorkModel========" + remoteWorkModel);
    }

    @Override
    public void initWidget() {
        mBtnCommit.setOnClickListener(this);
        datas = new ArrayList<>();
        //获取工作模式列表
        String token = UserInfoUtils.getInstance().getToken(mContext);
        mPresenter.getWorkMode(this, token, pageType);

        mListView.setOnItemClickListener((parent, view, position, id) -> {
            choosedPosition = position;
            datas.get(position).setChoosed(!datas.get(position).isChoosed());
            adapter.notifyDataSetChanged();
        });

        mBaseTitleBarView.addOnLeftTitleClickListener(this);
    }

    private void setListViewData() {
        if (null != datas && !datas.isEmpty()) {
            adapter = new CommonAdapter<WorkModeItemBean>(datas, R.layout.item_layout_switch_button_item) {
                @Override
                public void convert(ViewHolder holder, WorkModeItemBean bean) {
                    int position = holder.getPosition();

                    if (!TextUtils.isEmpty(bean.getDmsm())) {
                        holder.setText(R.id.tvName, bean.getDmsm());
                    }
                    if (choosedPosition == position) {
                        bean.setChoosed(true);
                        holder.setImageResource(R.id.ivChoose, R.drawable.radio_checked);
                    } else {
                        bean.setChoosed(false);
                        holder.setImageResource(R.id.ivChoose, R.drawable.radio_unchecked);
                    }
                    if (bean.isChoosed()) {
                    } else {
                    }
                }
            };
            mListView.setAdapter(adapter);
        }
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.title_left:
                finish();
                break;
            case R.id.btnCommit:
                int size = datas.size();
                WorkModeItemBean workModeItemBean = null;
                for (WorkModeItemBean bean : datas) {
                    if (bean.isChoosed()) {
                        workModeItemBean = bean;
                    }
                }
                if (workModeItemBean != null) {
                    String token = UserInfoUtils.getInstance().getToken(mContext);
                    //雾区编号
                    String dmlb = workModeItemBean.getDmlb();
                    //工作模式
                    String dm = workModeItemBean.getDm();
                    Log.e(TAG, "widgetClick: dm===" + dm + "        dmlb==" + dmlb);
                    mPresenter.http_WorkModeUpdate(mActivity, token, dmlb, dm, pageType);
                }
                break;
        }
    }

    @Override
    protected void initPresenter() {
        mPresenter.setView(this);
    }

    @Override
    public void onError(CommException e) {
        ToastUtil.showShort(mContext, getString(R.string.tip_toast_update_error));
    }

    @Override
    public void onResponse(String s) {
    }


    @Override
    public void getWorkMode(List<WorkModeItemBean> workModeItemBeanList) {
        Log.e(TAG, "getWorkMode: workModeItemBeanList======" + workModeItemBeanList);
        if (null != workModeItemBeanList && !workModeItemBeanList.isEmpty()) {
            datas.clear();
            int size = workModeItemBeanList.size();
            for (int i = 0; i < size; i++) {
                WorkModeItemBean bean = workModeItemBeanList.get(i);
                if (remoteWorkModel.equals(bean.getDm())) {
                    choosedPosition = i;
                    break;
                }
            }
            datas.addAll(workModeItemBeanList);
            setListViewData();
        }
    }

    @Override
    public void updtaeWorkMode(UpdateWorkModeResultBean resultBean) {
        if (null != resultBean) {
            if (resultBean.isKzzt()) {
                ToastUtil.showShort(mContext, getString(R.string.tip_toast_update_success));
                finish();//成功之后直接干掉页面！
            } else {
                ToastUtil.showShort(mContext, getString(R.string.tip_toast_update_error));
            }
        }
    }
}
